package cn.platform.service.impl;

import cn.platform.entity.Clazz;
import cn.platform.exception.BusinessException;
import cn.platform.mapper.ClazzMapper;
import cn.platform.mapper.StudentMapper;
import cn.platform.mapper.TeacherClassMapper;
import cn.platform.request.ClassQueryDTO;
import cn.platform.request.ClazzDTO;
import cn.platform.request.ClazzDetailDTO;
import cn.platform.response.ClassDetailVO;
import cn.platform.response.ClassVO;
import cn.platform.response.PageResult;
import cn.platform.response.TeacherOptionVO;
import cn.platform.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Year;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private TeacherClassMapper teacherClassMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询班级信息
     * @param classQueryDTO
     * @return
     */
    @Override
    public PageResult<ClassVO> page(ClassQueryDTO classQueryDTO) {
        if(classQueryDTO.getGrade()!=null){
            int currentYear = Year.now().getValue();
            classQueryDTO.setGradeYear(currentYear- classQueryDTO.getGrade()+1);
        }
        PageHelper.startPage(classQueryDTO.getPage(),classQueryDTO.getPageSize());
        Page<ClassVO> voList = clazzMapper.page(classQueryDTO);

        int currentYear = Year.now().getValue();
        for(ClassVO vo : voList){
            int grade = currentYear- vo.getGradeYear()+1;
            if(grade>=1 && grade<=6){
                vo.setGrade(grade);
            }else {
                vo.setGrade(null);
                vo.setClassNum(null);
            }
        }
        return new PageResult<>(voList.getTotal(),voList.getResult());
    }

    /**
     * 查看班级详细信息
     * @param id
     * @return
     */
    @Override
    public ClassDetailVO getClassDetail(String id) {
        ClazzDetailDTO clazz = clazzMapper.getClassById(id);
        ClassDetailVO classDetailVO = new ClassDetailVO();
        BeanUtils.copyProperties(clazz,classDetailVO);
        int currentYear = Year.now().getValue();
        int grade = currentYear-clazz.getGradeYear()+1;
        if(grade>=1&&grade<=6){
            classDetailVO.setGrade(grade);
        }else {
            classDetailVO.setGrade(null);
            classDetailVO.setClassNum(null);
        }

        List<TeacherOptionVO> teachers = teacherClassMapper.getByClassId(id);
        classDetailVO.setTeachers(teachers);
        return classDetailVO;
    }

    /**
     * 新增班级信息
     * @param addDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveClass(ClazzDTO addDTO) {
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(addDTO,clazz);
        int currentYear = Year.now().getValue();
        clazz.setGradeYear(currentYear-addDTO.getGrade()+1);
        clazzMapper.insert(clazz);
        if (addDTO.getTeachers()!=null && ! addDTO.getTeachers().isEmpty()){
            teacherClassMapper.insert(addDTO.getTeachers(),clazz.getId());
        }

    }

    /**
     * 更新班级信息
     * @param updateDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ClazzDTO updateDTO) {
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(updateDTO,clazz);
        int currentYear = Year.now().getValue();
        clazz.setGradeYear(currentYear-updateDTO.getGrade()+1);
        clazzMapper.update(clazz);

        if (updateDTO.getTeachers()!=null){
            teacherClassMapper.deleteByClassId(updateDTO.getId());
            teacherClassMapper.insert(updateDTO.getTeachers(), updateDTO.getId());
        }

    }

    /**
     * 删除班级信息
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        int count = studentMapper.getCountByClassId(id);
        if(count>0){
            throw new BusinessException("该班级下有学生，无法删除");
        }
        Integer gradeYear = clazzMapper.getGradeYearById(id);
        int grade = Year.now().getValue() - gradeYear + 1;
        if(grade>=1&&grade<=6){
            throw new BusinessException("该班未毕业，无法删除");
        }
        teacherClassMapper.deleteByClassId(id);
        clazzMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(List<String> ids) {
        List<Integer> gradeYears = clazzMapper.getGradeYearByIds(ids);
        gradeYears.forEach(gradeYear->{
            int grade = Year.now().getValue() - gradeYear + 1;
            if(grade>=1&&grade<=6){
                throw new BusinessException("选择的班级有未毕业班，无法删除");
            }
        });
        ids.forEach(id->{
            teacherClassMapper.deleteByClassId(id);
            clazzMapper.deleteById(id);
        });

    }
}
